package com.longtop.framework.sys.func.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.CacheUpdate;
import com.longtop.framework.common.Constant;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.sys.func.model.TSysFunc;

/**
 * 系统管理-功能service
 * 
 * @author SJH
 */
public class FuncService extends BaseService {

	public static final String BEAN_ID = "funcService";

	/**
	 * 提交保存
	 * 
	 * @param changeMap
	 * @return
	 */
	public Integer[] saveFunc(Map changeMap) {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;
		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysFunc sysFunc;// C
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				sysFunc = (TSysFunc) iterator.next();
				super.save(sysFunc);
				insertCount++;
			}
		}
		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				sysFunc = (TSysFunc) iterator.next();
				super.update(sysFunc);
				updateCount++;
			}
		}
		// 批量删除记录//C
		if (idList.length() > 0) {
			String sql = "delete from TSysFunc where funcId in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}
		
//		// 更新緩存：
//		getHibernateTemplate().flush();
//		CacheUpdate.getCacheUpdate().updateAuthOperFuncMap();
		
		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}

	/**
	 * 获取菜单列表
	 * 
	 * @return
	 */
	public List getMenuList() {
		return super.queryAll("from TSysMenu order by sortNo asc");
	}

	/**
	 * 依据条件，读取功能列表
	 * 
	 * @param queryConditions
	 * @return
	 */
	public QueryResult loadFunc(QueryConditions queryConditions) {
		queryConditions.setOrderBy("funcId asc");
		return super.loadByConditions(queryConditions, 1, -1, TSysFunc.class);
	}
}